<template>
	<view class="my-settle-container">
		<label class="radio" @click="changeAllState">
			<radio color="#ffd242" :checked="isFullCheck" /><text>全选</text>
		</label>
		<view class="amount-box">
			合计:<text class="price">￥{{checkedGoodsAmount}}</text>
		</view>
		<view class="settle-btn" @click="settlement">
			结算({{checkedCount}})
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		name: "my-settle",
		data() {
			return {
				time: 3,
				timer: null
			};
		},
		computed: {
			...mapGetters('m_cart', ['checkedCount', 'total', 'checkedGoodsAmount']),
			...mapGetters('m_user', ['addstr']), //user模块里的方法，里面包含地址
			...mapState('m_user', ['token']),
			...mapState('m_cart', ['cart']),
			// 是否全选
			isFullCheck() {
				return this.total === this.checkedCount
			}
		},
		methods: {
			// 使用 mapMutations 辅助函数，把 m_cart 模块提供的 updateAllGoodsState 方法映射到当前组件中使用
			...mapMutations('m_cart', ['updateAllGoodsState']),
			...mapMutations('m_user', ['updateRedirectInfo']),
			// 全选处理函数
			changeAllState() {
				// 修改购物车中所有商品的选中状态
				this.updateAllGoodsState(!this.isFullCheck)
			},
			// 结算条件判断
			settlement() {
				// 先判断用户是否勾选了要结算的商品
				if (!this.checkedCount) {
					return uni.$showMsg('请选择要结算的商品')
				}
				//再判断用户是否选择了收货地址 =========== 暂时停用
				if (!this.addstr) {
					return uni.$showMsg('请选择收货地址')
				}
				// 最后判断用户是否登录
				if (!this.token) {
					// return uni.$showMsg('请先登录')	
					this.delayNavigate()
				}
				this.payOrder()
			},
			// 支付
			async payOrder() {
				// 1.创建订单
				// 1.1 编辑订单的信息对象
				const orderInfo = {
					// order_price: this.checkedGoodsAmount, 
					order_price: '0.01', //开发环境
					consignee_addr: this.addstr,
					goods: this.cart.filter(x => x.goods_state).map(x => ({
						goods_id: x.goods_id,
						goods_number: x.goods_count,
						goods_price: x.goods_price
					}))
				};
				// 1.2 发起请求提交订单信息
				const {
					data: res
				} = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo);
				if (res.meta.status !== 200) {
					return uni.$showMsg('创建订单失败')
				}
				// 1.3 接收服务器返回的订单编号
				const orderNumber = res.message.order_number
				console.log('订单编号', orderNumber)
				// 2.订单预支付
				// 2.获取订单支付信息
				const {
					data: res2
				} = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', {
					order_number: orderNumber
				})
				console.log('预支付返回信息', res2);
				if (res2.meta.status !== 200) return uni.$showMsg('预支付订单生成失败')
				const payInfo = res2.message.pay
				const [
					err,
					succ
				] = await uni.requestPayment(payInfo)
				if (err) return uni.$showMsg('订单未支付')
				// 完成支付后根据订单编号检查支付结果
				const {
					data: res3
				} = await uni.$http.post('/api/public/v1/my/orders/chkOrder', {
					order_number: orderNumber
				})
				if (res3.meta.status !== 200) return uni.$showMsg('订单未支付')
				uni.showToast({
					title: '订单支付完成',
					icon: 'success'
				})
				console.log("支付成功的信息", succ, res3);
			},
			// 结算登录提示
			showTips(n) {
				uni.showToast({
					icon: 'none',
					title: '请登录后再结算！' + n + '秒后自动跳转到登录页',
					// 为页面添加透明遮罩，防止点击穿透
					mask: true,
					duration: 1000
				})
			},
			// 延迟跳转到登录界面
			delayNavigate() {
				this.timer = setInterval(() => {
					this.time--
					console.log(this.time);
					this.showTips(this.time)
					if (this.time <= 0) {
						clearInterval(this.timer)
						uni.switchTab({
							url: '/pages/my/my',
							success: () => {
								this.updateRedirectInfo({
									openType: 'switchTab',
									from: '/pages/cart/cart'
								})
							}
						})
					}
					return
				}, 1000)
			},
		},
	}
</script>

<style lang="scss">
	.my-settle-container {
		position: fixed;
		/*#ifdef H5*/
		bottom: 3rem;
		/*#endif */
		/*#ifdef MP-WEIXIN*/
		bottom: 0;
		/*#endif */
		left: 0;
		right: 0;
		// width: 100%;
		height: 3.125rem;
		z-index: 99;
		background-color: white;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 5px;
		font-size: 14px;

		.radio {
			display: flex;
			align-items: center;
		}

		.price {
			color: #ffd242;
		}

		.settle-btn {
			height: 50px;
			min-width: 100px;
			background-color: #ffd242;
			color: white;
			line-height: 50px;
			text-align: center;
			padding: 0 10px;
		}
	}
</style>
